// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import TushareModule from "../../module/TushareModule";
import DropList from "../../views/items/DropList";
import Top10HolderItem from "../items/Top10HolderItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Top10HoldersComponent extends cc.Component {

    @property(DropList)
    dropType: DropList = null;

    @property(DropList)
    dropDate: DropList = null;
    
    @property(cc.Node)
    content: cc.Node = null;

    @property(cc.Prefab)
    holderItemPrefab: cc.Prefab = null;

    _holders: any = [];

    _holderItems: any = [];

    ts_code: string = '';

    typeKey: string = '';
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.dropType.setData(['十大流通股东', '十大股东'], 0);
        this.dropType.setSelectCallback(()=>{
            this.updateView();
        });
        this.dropDate.setSelectCallback(()=>{
            this.updateView();
        });
    }

    calcHolders (list) {
        let holders = {};
        let dates = [];
        if (list == null || list.length == null) {
            return {holderMap: holders, dates: dates}; 
        }
        for (let index = 0; index < list.length; index++) {
            const item = list[index];
            let date = item.end_date;
            if (holders[date] == null) {
                holders[date] = [];
                dates.push(date);
            }
            holders[date].push(item);
        }

        dates.sort(function (a, b) {
            let aNum = parseInt(a);
            let bNum = parseInt(b);
            return bNum -aNum;
        });

        for (let index = 0; index < dates.length; index++) {
            const date = dates[index];
            let preArray = null;
            if (index < dates.length - 1) {
                let datePre = dates[index + 1];
                preArray = holders[datePre];
            }
            let array = holders[date];
            for (let index = 0; index < array.length; index++) {
                const holder = array[index];
                if (preArray) {
                    let holdPre = null;
                    for (let index = 0; index < preArray.length; index++) {
                        const holderItem = preArray[index];
                        if (holderItem.holder_name == holder.holder_name) {
                            holdPre = holderItem;
                            break;
                        }
                    }
                    if (holdPre == null) {
                        holder.changeType = '新进';
                    } else {
                        if (holder.hold_amount == holdPre.hold_amount) {
                            holder.changeType = '不变';
                        } else if (holder.hold_amount > holdPre.hold_amount) {
                            holder.changeType = '增持';
                        } else {
                            holder.changeType = '减持';
                        }
                    }
                } else {
                    holder.changeType = '不变';
                }
            }
            
            array.sort(function (a, b) {
                return b.hold_amount - a.hold_amount;
            });
        }
        return {holderMap: holders, dates: dates};
    }

    clearData () {
        for (let index = 0; index < this._holderItems.length; index++) {
            const comp = this._holderItems[index];
            comp.node.active = false;
        }
    }

    setData (top10Data: any) {
        this.clearData();
        if (top10Data == null) return;
        let holders = this.calcHolders(top10Data.top10);
        let floatHolders = this.calcHolders(top10Data.top10_float);
        this._holders = [floatHolders, holders];
        this.ts_code = top10Data.ts_code;
        this.updateView();
    }

    updateView () {
        let type = this.dropType.getSelectIndex();
        let keys = ['float', 'total'];
        this.typeKey = keys[type];
        let holders = this._holders[type];
        if (holders.dates == null || holders.dates.length == 0) return;
        let dates = holders.dates;
        let index = this.dropDate.getSelectIndex();
        this.dropDate.setData(dates, index);
        index = this.dropDate.getSelectIndex();
        let date = dates[index];
        let holderList = holders.holderMap[date];
        this.updateContent(holderList);
    }

    updateContent (holderList) {
        for (let index = 0; index < holderList.length; index++) {
            const holder = holderList[index];
            let itemComp = this._holderItems[index];
            if (itemComp == null) {
                let node = cc.instantiate(this.holderItemPrefab);
                this.content.addChild(node);
                itemComp = node.getComponent(Top10HolderItem);
                this._holderItems[index] = itemComp;
            }
            itemComp.node.active = true;

            let strData = this.calcStrData(holder);
            itemComp.setItemData(holder.holder_name, strData);
        }
    }

    // fields: 'ts_code,close,pe,pe_ttm,pe,pe_ttm,pb,total_mv,circ_mv,float_share,total_share'
    calcStrData (holder: any) {
        let market = TushareModule.getInstance().dailyMarket[this.ts_code];
        let str = '';
        str = '';
        str += '持股:' + (Math.round(holder.hold_amount / 100) / 100) + '万';
        str += '  占比:' + (Math.round(holder.hold_amount / market[this.typeKey + '_share']) / 100) + '%';
        str += '  当前价总值:' + (Math.round(holder.hold_amount * market.close / 10000)) + '万元';
        str += '  ' + holder.changeType;
        return str;      
    }

    // update (dt) {}
}
